<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品属性" prop="productAttribute">
        <el-input
          v-model="queryParams.productAttribute"
          placeholder="请输入产品属性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品类型" prop="productType">
        <el-select v-model="queryParams.productType" placeholder="请选择产品类型" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户品牌" prop="customerBrand">
        <el-input
          v-model="queryParams.customerBrand"
          placeholder="请输入客户品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specModel">
        <el-input
          v-model="queryParams.specModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获取方式" prop="acquisitionMethod">
        <el-input
          v-model="queryParams.acquisitionMethod"
          placeholder="请输入获取方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM编码" prop="bomCode">
        <el-input
          v-model="queryParams.bomCode"
          placeholder="请输入BOM编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="versionNumber">
        <el-input
          v-model="queryParams.versionNumber"
          placeholder="请输入版本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本类型" prop="versionType">
        <el-select v-model="queryParams.versionType" placeholder="请选择版本类型" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品版本校验" prop="versionCheck">
        <el-input
          v-model="queryParams.versionCheck"
          placeholder="请输入产品版本校验"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="PCS换算率" prop="pcsConversionRate">
        <el-input
          v-model="queryParams.pcsConversionRate"
          placeholder="请输入PCS换算率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM标准价格" prop="bomStandardPrice">
        <el-input
          v-model="queryParams.bomStandardPrice"
          placeholder="请输入BOM标准价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PCS标准单价" prop="pcsStandardUnitPrice">
        <el-input
          v-model="queryParams.pcsStandardUnitPrice"
          placeholder="请输入PCS标准单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PCS人工单价" prop="pcsLaborUnitPrice">
        <el-input
          v-model="queryParams.pcsLaborUnitPrice"
          placeholder="请输入PCS人工单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PCS标准水电价" prop="pcsWaterElectricityPrice">
        <el-input
          v-model="queryParams.pcsWaterElectricityPrice"
          placeholder="请输入PCS标准水电价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最新版本号" prop="latestVersion">
        <el-input
          v-model="queryParams.latestVersion"
          placeholder="请输入最新版本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子产品数" prop="subProductCount">
        <el-input
          v-model="queryParams.subProductCount"
          placeholder="请输入子产品数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:master:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:master:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-refresh"
            size="mini"
            @click="handleSync"
            v-hasPermi="['md:master:sync']"
        >数据同步</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:master:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:master:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="masterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="serialNum" />
      <el-table-column label="BOM主键ID" align="center" prop="bomId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="产品属性" align="center" prop="productAttribute" />
      <el-table-column label="产品类型" align="center" prop="productType" />
      <el-table-column label="客户品牌" align="center" prop="customerBrand" />
      <el-table-column label="规格型号" align="center" prop="specModel" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="获取方式" align="center" prop="acquisitionMethod" />
      <el-table-column label="BOM编码" align="center" prop="bomCode" />
      <el-table-column label="版本号" align="center" prop="versionNumber" />
      <el-table-column label="版本类型" align="center" prop="versionType" />
      <el-table-column label="产品版本校验" align="center" prop="versionCheck" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="PCS换算率" align="center" prop="pcsConversionRate" />
      <el-table-column label="BOM标准价格" align="center" prop="bomStandardPrice" />
      <el-table-column label="PCS标准单价" align="center" prop="pcsStandardUnitPrice" />
      <el-table-column label="PCS人工单价" align="center" prop="pcsLaborUnitPrice" />
      <el-table-column label="PCS标准水电价" align="center" prop="pcsWaterElectricityPrice" />
      <el-table-column label="产品图片" align="center" prop="productImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.productImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="最新版本号" align="center" prop="latestVersion" />
      <el-table-column label="子产品数" align="center" prop="subProductCount" />
      <el-table-column label="子产品名称集合" align="center" prop="subProductNames" />
      <el-table-column label="子产品编码集合" align="center" prop="subProductCodes" />
      <el-table-column label="子产品用量集合" align="center" prop="subProductQuantities" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:master:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:master:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改BOM主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="产品属性" prop="productAttribute">
          <el-input v-model="form.productAttribute" placeholder="请输入产品属性" />
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-select v-model="form.productType" placeholder="请选择产品类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="客户品牌" prop="customerBrand">
          <el-input v-model="form.customerBrand" placeholder="请输入客户品牌" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specModel">
          <el-input v-model="form.specModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="获取方式" prop="acquisitionMethod">
          <el-input v-model="form.acquisitionMethod" placeholder="请输入获取方式" />
        </el-form-item>
        <el-form-item label="BOM编码" prop="bomCode">
          <el-input v-model="form.bomCode" placeholder="请输入BOM编码" />
        </el-form-item>
        <el-form-item label="版本号" prop="versionNumber">
          <el-input v-model="form.versionNumber" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="版本类型" prop="versionType">
          <el-select v-model="form.versionType" placeholder="请选择版本类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="产品版本校验" prop="versionCheck">
          <el-input v-model="form.versionCheck" placeholder="请输入产品版本校验" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="PCS换算率" prop="pcsConversionRate">
          <el-input v-model="form.pcsConversionRate" placeholder="请输入PCS换算率" />
        </el-form-item>
        <el-form-item label="BOM标准价格" prop="bomStandardPrice">
          <el-input v-model="form.bomStandardPrice" placeholder="请输入BOM标准价格" />
        </el-form-item>
        <el-form-item label="PCS标准单价" prop="pcsStandardUnitPrice">
          <el-input v-model="form.pcsStandardUnitPrice" placeholder="请输入PCS标准单价" />
        </el-form-item>
        <el-form-item label="PCS人工单价" prop="pcsLaborUnitPrice">
          <el-input v-model="form.pcsLaborUnitPrice" placeholder="请输入PCS人工单价" />
        </el-form-item>
        <el-form-item label="PCS标准水电价" prop="pcsWaterElectricityPrice">
          <el-input v-model="form.pcsWaterElectricityPrice" placeholder="请输入PCS标准水电价" />
        </el-form-item>
        <el-form-item label="产品图片">
          <image-upload v-model="form.productImage"/>
        </el-form-item>
        <el-form-item label="最新版本号" prop="latestVersion">
          <el-input v-model="form.latestVersion" placeholder="请输入最新版本号" />
        </el-form-item>
        <el-form-item label="子产品数" prop="subProductCount">
          <el-input v-model="form.subProductCount" placeholder="请输入子产品数" />
        </el-form-item>
        <el-form-item label="子产品名称集合" prop="subProductNames">
          <el-input v-model="form.subProductNames" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="子产品编码集合" prop="subProductCodes">
          <el-input v-model="form.subProductCodes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="子产品用量集合" prop="subProductQuantities">
          <el-input v-model="form.subProductQuantities" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标志 (0:存在 2:删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志 (0:存在 2:删除)" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaster, getMaster, delMaster, addMaster, updateMaster,bomSync } from "@/api/mes/md/bomdingding";

export default {
  name: "Master",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BOM主表格数据
      masterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,        productCode: null,        productAttribute: null,        productType: null,        customerBrand: null,        specModel: null,        unit: null,        acquisitionMethod: null,        bomCode: null,        versionNumber: null,        versionType: null,        versionCheck: null,        status: null,        pcsConversionRate: null,        bomStandardPrice: null,        pcsStandardUnitPrice: null,        pcsLaborUnitPrice: null,        pcsWaterElectricityPrice: null,        productImage: null,        latestVersion: null,        subProductCount: null,        subProductNames: null,        subProductCodes: null,        subProductQuantities: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bomCode: [
          { required: true, message: "BOM编码不能为空", trigger: "blur" }
        ],      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询BOM主列表 */
    getList() {
      this.loading = true;
      listMaster(this.queryParams).then(response => {
        this.masterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialNum: null,        bomId: null,        productName: null,        productCode: null,        productAttribute: null,        productType: null,        customerBrand: null,        specModel: null,        unit: null,        acquisitionMethod: null,        bomCode: null,        versionNumber: null,        versionType: null,        versionCheck: null,        status: "0",        pcsConversionRate: null,        bomStandardPrice: null,        pcsStandardUnitPrice: null,        pcsLaborUnitPrice: null,        pcsWaterElectricityPrice: null,        productImage: null,        latestVersion: null,        subProductCount: null,        subProductNames: null,        subProductCodes: null,        subProductQuantities: null,        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        remark: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNum)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加BOM主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialNum = row.serialNum || this.ids
      getMaster(serialNum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改BOM主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNum != null) {
            updateMaster(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaster(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSync() {
      if (this.loading) {
        this.$modal.message("正常进行中，请不要重复点击...");
        return;
      }
      this.loading = true;
      this.$modal.confirm('是否确认同步数据？').then(() => {
        bomSync().then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess("同步成功");
            this.getList();
          } else {
            this.$modal.msgError("同步失败");
          }
        }).catch(() => {
          this.$modal.msgError("同步失败");
        }).finally(() => {
          this.loading = false;
        });
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNums = row.serialNum || this.ids;
      this.$modal.confirm('是否确认删除BOM主编号为"' + serialNums + '"的数据项？').then(function() {
        return delMaster(serialNums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/master/export', {
        ...this.queryParams
      }, `master_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
